/**
 * Copyright 2009-2011 xiaonei.com All right reserved.
 * This software is the confidential and proprietary information of Xiaonei.com ("Confidential Information"). 
 * You shall not disclose such Confidential Information and shall use it only in accordance with the terms of the license agreement you entered into with Xiaonei.com.
 * Create by XiaoNeiAntiSpamTeam
 *
 */

package com.xiaonei.testzookeeper;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs.Ids;

/**
 * @ClassName: ZooKeeperOperator
 * @Description: TODO
 * @author shaohui.liu shaohui.liu@opi-corp.com
 * @date Feb 22, 2010 6:14:15 PM
 * 
 */
public class ZooKeeperOperator extends AbstractZooKeeper {

    public ZooKeeperOperator(String hosts) {
        super(hosts);
    }

    /**
     * 创建持久态的znode,比支持多层创建.比如在创建/parent/child的情况下,无/parent.无法通过.
     * 
     * @param path eg: /parent/child1
     * @param data
     * @throws InterruptedException
     * @throws KeeperException
     */
    public void create(String path, byte[] data) throws KeeperException, InterruptedException {
        this.zooKeeper
                .create(path, data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT/*此处创建的为持久态的节点,可为瞬态*/);
    }

    /**
     * 获取节点的孩子信息
     * 
     * @param path
     * @throws KeeperException
     * @throws InterruptedException
     */
    public void getChild(String path) throws KeeperException, InterruptedException {
        try {
            List<String> children = this.zooKeeper.getChildren(path, false);

            if (children.isEmpty()) {
                System.out.printf("没有节点在%s中.", path);
                return;
            } else {
                System.out.printf("节点%s中存在的节点:\n", path);
                for (String child : children) {
                    System.out.println(child);
                }
            }
        } catch (KeeperException.NoNodeException e) {
            System.out.printf("%s节点不存在.", path);
            throw e;
        }
    }

    /**
     * 
     * @Title: getData
     * @Description: 获取数据
     * @param @param path
     * @param @return
     * @param @throws KeeperException
     * @param @throws InterruptedException
     * @return byte[]
     * @throws
     */
    public byte[] getData(String path) throws KeeperException, InterruptedException {
        return this.zooKeeper.getData(path, false, null);
    }

    public static void main(String[] args) throws IOException, InterruptedException,
            KeeperException {

        ZooKeeperOperator zkoperator = new ZooKeeperOperator("10.3.20.147:2181,10.3.20.148:2181");

        byte[] data = new byte[] { 'd', 'a', 't', 'a' };

        zkoperator.create("/root", null);
        System.out.println(Arrays.toString(zkoperator.getData("/root")));

        zkoperator.create("/root/child1", data);
        System.out.println(Arrays.toString(zkoperator.getData("/root/child1")));

        zkoperator.create("/root/child2", data);
        System.out.println(Arrays.toString(zkoperator.getData("/root/child2")));

        System.out.println("节点孩子信息:");

        zkoperator.getChild("/root");

        zkoperator.close();
    }
}
